YUI.add('pageview', function (Y) {
	
	
    function PageView(config) {
    	PageView.superclass.constructor.apply(this, arguments);
    }

    
    PageView.NAME = 'pageview';
    
    
    Y.extend(PageView, Y.View, {
    	container: Y.one('#' + PageView.NAME),
    	
    	xslDocument: null, 
    	
    	events: {
    		
    	},
    	
    	initializer: function () {
    		// Add an event listener to automatically render this view when the pageid attribute change
    		this.model.on('pageidChange', this.render, this);
    	},
    	
    	
    	render: function () {
    	    if (this.xslDocument === null) {
    	    	try {
        	    	Y.io('page.xsl', {
        	    		context: this,
        	    		on : {
        	    	    	complete : function (transaction, response) {
        	    	        	this.xslDocument = response.responseXML;
        	    	        	this.loadPage();    	    	        	
        	    	        }
        	    	    }
        	    	});    	    		
    	    	} catch (e) {
    	    		alert(e);
    	    	}
    	    	
    	        	
    	    } else {
    	    	this.loadPage();
    	    }
    	        		
    		return this;
    	},
    	
    	
    	loadPage: function () {
        	Y.io(this.model.getSelectedPath(), {
        		context: this,
        		on: {
        			complete: function (transaction, response) {
            			this.renderPage(response.responseXML);
            		}
        		}
        	});    		
    	},
    	
    	
    	renderPage: function (xml) {
			var xsltProcessor = new XSLTProcessor(),
				pageElement = document.getElementById("page");
			
			if (pageElement.hasChildNodes()) {
			    while (pageElement.childNodes.length >= 1) {
			    	pageElement.removeChild(pageElement.firstChild);
			    }
			}
			
			xsltProcessor.importStylesheet(this.xslDocument);
			//xsltProcessor.setParameter(null, "path", path);
			
			pageElement.appendChild(
					xsltProcessor.transformToFragment(xml, document));
    	}
    });
    
    Y.namespace('srcprojector').PageView = PageView;
    

}, '0.1.0', {requires: ['view', 'io-base', 'tabview']});